﻿// This file is part of GEOTRON.

// GEOTRON is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The GEOTRON library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with GEOTRON.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

namespace Geotron
{
    public class Country
    {
        public string Name { set; get; }
        public string ShortName { set; get; }
        public bool GnsFile { set; get; }
        public bool PostCodeFile { set; get; }
        List<string> _PostcodeFormats;
        public List<string> PostcodeFormats {
            set { this._PostcodeFormats = value; }
            get
            {
                if(_PostcodeFormats == null) _PostcodeFormats = new List<string>();
                return _PostcodeFormats;
            }
        }

        public bool isThisAPostcode(string Postcode)
        {
            //Postcode = Postcode.Replace(" ","");
            for (int i = 0; i < PostcodeFormats.Count; i++)
            {
                // If their lengths are different, then it can't be valid.
                if (Postcode.Length == PostcodeFormats[i].Length)
                {
                    Trace.WriteLine("Now validating postcode '"+Postcode+"' with schema '"+PostcodeFormats[i]+"'");
                    bool isValid = true;
                    int j = 0;
                    while(isValid)
                    {
                        if (j >= PostcodeFormats[i].Length) break;
                        if (PostcodeFormats[i][j] == '#')
                        {
                            Trace.Write("Character "+j.ToString()+" is supposed to be a number...");
                            try { int number = Int32.Parse(Postcode[j].ToString()); Trace.WriteLine("And it is."); }
                            catch { isValid = false; Trace.WriteLine("But it's not."); }
                        }
                        else if (PostcodeFormats[i][j] == 'X')
                        {
                            Trace.Write("Character "+j.ToString()+" is supposed to be a letter...");
                            try { int number = Int32.Parse(Postcode[j].ToString()); Trace.WriteLine("But it's not."); }
                            catch { isValid = true; Trace.WriteLine("And it is."); }
                        }
                        j++;
                    }

                    if (isValid == true) return true;
                    else Trace.WriteLine("Validation Failure! (Not an " + Name + " postcode)"); ;
                }
            }
            return false;
        }
    }
}
